package org.zoomdev.zoom.dao.driver.h2;

import org.zoomdev.zoom.common.filter.Filter;
import org.zoomdev.zoom.dao.Entity;
import org.zoomdev.zoom.dao.EntityField;
import org.zoomdev.zoom.dao.driver.mysql.MysqlDriver;
import org.zoomdev.zoom.dao.impl.SimpleSqlBuilder;
import org.zoomdev.zoom.dao.impl.TableBuildInfo;
import org.zoomdev.zoom.dao.meta.ColumnMeta;
import org.zoomdev.zoom.dao.meta.DefaultValue;

import java.util.List;
import java.util.Map;

public class H2Driver extends MysqlDriver {

    @Override
    public void insertOrUpdate(StringBuilder sb, List<Object> values, String tableName, Map<String, Object> data, String... unikeys) {
        throw new UnsupportedOperationException("insertOrUpdate is not supported in h2");
    }

    @Override
    public void buildTable(TableBuildInfo table, List<String> sqlList) {
        super.buildTable(table, sqlList);

        buildComment(table, sqlList);
    }

    @Override
    public <T> void buildInsertOrUpdate(SimpleSqlBuilder builder, Entity entity, T data, Filter<EntityField> filter, boolean ignoreNull, String[] unikeys) {
        throw new UnsupportedOperationException("insertOrUpdate is not supported in h2");
    }

    @Override
    protected void buildDefaultValue(ColumnMeta columnMeta, StringBuilder sb) {
        if (columnMeta.getDefaultValue() instanceof DefaultValue) {
            Object value = ((DefaultValue) columnMeta.getDefaultValue()).getValue();
            if (value.equals(MysqlDriver.CURRENT_TIMESTAMP_ON_UPDATE)) {
                columnMeta.setDefaultValue(DefaultValue.function(MysqlDriver.CURRENT_TIMESTAMP));
            }
        }
        super.buildDefaultValue(columnMeta, sb);
    }

    @Override
    protected void createColumnComment(StringBuilder sb, ColumnMeta columnMeta) {

    }

    @Override
    protected void createTableComment(StringBuilder sb, TableBuildInfo table) {

    }
}
